/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.finder.windows;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.WmiUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-23 上午10:42:07          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class WMIFinder extends Finder {
    private static final Logger logger = Logger.getLogger(WMIFinder.class);
    private static final String SEPARATOR = " <@> ";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] ss = null;
        BufferedReader reader = null;
        String strLine = null;
        String scriptCmd = (String) params.get("SCRIPT");
        reader = WmiUtil.execWmiScript(ip, scriptCmd, null);
        try {
            while ((strLine = reader.readLine()) != null) {
                boolean flag = false;
                String[] wmiInfo = null;
                if (strLine.indexOf(SEPARATOR) > -1) {
                    flag = true;
                    //将由于特殊字符产生的乱码去掉,只保留汉字，字符，-，<,@,>, ,\
                    String regex = "[^\\[\u4e00-\u9fa5\\]\\w\\-<@>\\s:/\\\\]";
                    strLine = strLine.replaceAll(regex, "");
                    wmiInfo = strLine.split(SEPARATOR);
                }
                if (flag) {
                    ss = new String[wmiInfo.length + 3];
                    System.arraycopy(wmiInfo, 0, ss, 3, wmiInfo.length);
                    String id = (String) params.get("ID");
                    String name = (String) params.get("NAME");
                    String description = (String) params.get("DESC");
                    //替换$+\\d
                    id = replace(id, wmiInfo);
                    name = replace(name, wmiInfo);
                    description = replace(description, wmiInfo);
                    ss[0] = id;
                    ss[1] = name;
                    ss[2] = description;
                    list.add(ss);
                }
            }
        } catch (IOException e) {
            logger.error("failed to execuit WmiScript" + (String) params.get("SCRIPT"), e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return list;
    }

    private String replace(String str, String[] wmiInfo) {
        Pattern pattern = Pattern.compile("^.*\\$(\\d+).*$");
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            int index = Integer.parseInt(matcher.group(1));
            if (index <= wmiInfo.length) {
                str = str.replace("$" + index, wmiInfo[index - 1]);
            }
        }
        return str;
    }
}
